package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.TerLishuaVipDao;
import com.framework.dto.LishuaVipDTO;
import com.framework.entity.flow.FlowVipCashBack;
import com.framework.entity.ter.TerLishuaVip;
import com.framework.entity.ter.TerMerchantReport;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.entity.tran.TransactionInputLishua;
import com.framework.service.*;
import com.framework.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wq
 * @date 2019/10/23 19:41
 * @description
 */
@Service
@Slf4j
public class TerLishuaVipServiceImpl extends ServiceImpl<TerLishuaVipDao, TerLishuaVip> implements TerLishuaVipService {


    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private TransactionInputLishuaService transactionInputLishuaService;

    @Autowired
    private AgentInfoService agentInfoService;

    @Autowired
    private FlowAccountService flowAccountService;

    @Autowired
    private MerchantService merchantService;


    @Override
    public Map<String, String> updateLishuaVip(LishuaVipDTO lishuaVipDTO) {
        Map<String, String> result = new HashMap<>();


        if (lishuaVipDTO.getMemberType().intValue() != 1) {
            result.put("ret_code", "00");
            result.put("ret_msg", "success");
            return result;
        }

        TransactionInputLishua lishua = transactionInputLishuaService.selectById(lishuaVipDTO.getOrderId());

        if (lishua == null) {
            result.put("ret_code", "00");
            result.put("ret_msg", "success");
            return result;
        }

        String sn = lishuaVipDTO.getPossn();

        TerTerminalInfo terminalInfo = terminalInfoService.queryTerminalBySn(sn);

        if (terminalInfo == null) {
            result.put("ret_code", "00");
            result.put("ret_msg", "success");
            return result;
        }

        TerLishuaVip lishuaVip = queryBySn(sn);

        String startStr = lishuaVipDTO.getStartTime();
        String endStr = lishuaVipDTO.getEndTime();

        Date startDate = DateUtils.stringToDate(startStr, DateUtils.DATE_TIME_PATTERN);

        Date endDate = DateUtils.stringToDate(endStr, DateUtils.DATE_TIME_PATTERN);


        if (lishuaVip == null) {
            lishuaVip = new TerLishuaVip();
            lishuaVip.setEndDate(startDate);
            lishuaVip.setStartDate(endDate);
            lishuaVip.setSn(sn);
            insert(lishuaVip);
        } else {
            lishuaVip.setEndDate(startDate);
            lishuaVip.setStartDate(endDate);
            updateById(lishuaVip);
        }

        //处理Vip
        addVipCash(terminalInfo, lishuaVipDTO);


        result.put("ret_code", "00");
        result.put("ret_msg", "success");

        return result;
    }

    private void addVipCash(TerTerminalInfo info, LishuaVipDTO dto) {
        //查询是否以奖励
        //处理时间
        Date date = DateUtils.stringToDate(dto.getStartTime(), DateUtils.DATE_TIME_PATTERN);

        String vipTime = DateUtils.format(date, DateUtils.DATE_PATTERN);

        Date doVipTime = DateUtils.stringToDate(vipTime, DateUtils.DATE_PATTERN);
        FlowVipCashBack back = flowAccountService.queryVipBySn(info.getSn(), doVipTime);

        if (back != null) {
            return;
        }
        TerMerchantReport report = merchantService.queryMerchantBySn(info.getSn());

        if (report == null) {
            return;
        }

        BigDecimal amount = getVipAmount(Integer.valueOf(dto.getOpMon()));

        if(amount==null){
            log.warn("----立刷vip返现失败,sn:"+info.getSn()+"--持续月份:"+dto.getOpMon()+"---------------------");
            return;
        }

        FlowVipCashBack vip = new FlowVipCashBack();
        vip.setMerchantNo(info.getMerchantCode());
        vip.setSn(info.getSn());
        vip.setDoVipTime(doVipTime);
        vip.setCreateTime(new Date());
        vip.setAgentId(report.getAgentId());
        vip.setMoney(amount);
        agentInfoService.addAmount(report.getAgentId(),amount);
        flowAccountService.addVipCashAccount(vip,8);

    }

    private BigDecimal getVipAmount(Integer opMon) {
        switch (opMon) {
            case 3:
                return new BigDecimal("299");
            case 6:
                return new BigDecimal("499");
            case 12:
                return new BigDecimal("599");
            default:
                return null;
        }
    }

    @Override
    public TerLishuaVip queryBySn(String sn) {
        return selectOne(new EntityWrapper<TerLishuaVip>().eq("sn", sn));
    }


}
